package com.i9he.ping;

import static com.i9he.ping.utils.ConfigUtils.configuration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.i9he.ping.service.IPStrategyHandler;
import com.i9he.ping.service.impl.MailThread;
import com.i9he.ping.service.impl.PingThread;
import com.i9he.ping.service.impl.StrategyImpl;

public class App {

	static int max = 0;

	public static void main(String[] args) {

		List<String> iplist = Arrays.asList(configuration.getString("ip.list").split(";"));
		List<Thread> threads = new ArrayList<Thread>();

		// 开启Email线程扫描
		MailThread mailThread = new MailThread();
		IPStrategyHandler ipStrategyHandler = mailThread;
		mailThread.start();

		for (String ip : new HashSet<String>(iplist)) { // 去除重复IP
			if (StringUtils.isBlank(ip)) {
				continue;
			}
			PingThread t = new PingThread(ip.trim());
			t.setStrategy(new StrategyImpl(ipStrategyHandler));
			t.start();
			threads.add(t);
		}

		for (Thread t : threads) {
			try {
				t.join();//把ip线程加入到主线程，优先执行ip线程
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			mailThread.join();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
}
